package com.chkj.fbook.system.controller;

import java.util.List;
import java.util.Map;

import com.chkj.fbook.common.utils.PageUtils;
import com.chkj.fbook.common.utils.Query;
import com.chkj.fbook.common.utils.R;
import com.chkj.fbook.system.service.AppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chkj.fbook.common.annotation.Log;
import com.chkj.fbook.common.annotation.Permission;
import com.chkj.fbook.system.domain.AppDO;

/**
 * 应用表
 * 
 * @author shh_he
 * @email 980027164@qq.com
 * @date 2023-10-2 17:12:56
 */
 
@Controller
@RequestMapping("/system/app")
public class AppController {

	@Autowired
	private AppService appService;
	
	@Log("应用表-去到列表页面")
	@GetMapping()
	@Permission(name = "system:app:app")
	public String App(){
	    return "system/app/app";
	}
	
	@Log("应用表-查询列表数据")
	@ResponseBody
	@GetMapping("/list")
	@Permission(name = "system:app:app")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<AppDO> appList = appService.list(query);
		int total = appService.count(query);
		PageUtils pageUtils = new PageUtils(appList, total);
		return pageUtils;
	}
	
	@Log("应用表-去到添加页面")
	@GetMapping("/add")
	@Permission(name = "system:app:add")
	public String add(){
	    return "system/app/add";
	}

	@Log("应用表-去到修改页面")
	@GetMapping("/edit/{uuid}")
	@Permission(name = "system:app:edit")
	public String edit(@PathVariable("uuid") String uuid,Model model){
		AppDO app = appService.get(uuid);
		model.addAttribute("app", app);
	    return "system/app/edit";
	}
	
	/**
	 * 保存
	 */
	@Log("应用表-保存操作")
	@ResponseBody
	@PostMapping("/save")
	@Permission(name = "system:app:add")
	public R save(AppDO app){
		boolean exists = appService.isExists(app);
		if(exists) {
			return R.error(1, "应用代码或名称已存在！");
		}else if(appService.save(app)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@Log("应用表-修改操作")
	@ResponseBody
	@RequestMapping("/update")
	@Permission(name = "system:app:edit")
	public R update( AppDO app){
		boolean exists = appService.isExists(app);
		if(exists) {
			return R.error(1, "应用代码或名称已存在！");
		}
		appService.update(app);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@Log("应用表-删除")
	@PostMapping( "/remove")
	@ResponseBody
	@Permission(name = "system:app:remove")
	public R remove( String uuid){
		if(appService.remove(uuid)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@Log("应用表-批量删除")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@Permission(name = "system:app:batchRemove")
	public R remove(@RequestParam("ids[]") String[] uuids){
		appService.batchRemove(uuids);
		return R.ok();
	}
	
}
